<?php

namespace Xin\EasyQueue\Hyperf;

use Hyperf\AsyncQueue\Driver\DriverFactory;
use Xin\EasyQueue\Contracts\Dispatcher;

class RedisDispatcher implements Dispatcher
{
    /**
     * @var DriverFactory
     */
    protected DriverFactory $driverFactory;

    /**
     * @param DriverFactory $driverFactory
     */
    public function __construct(DriverFactory $driverFactory)
    {
        $this->driverFactory = $driverFactory;
    }

    /**
     * @param array $options
     * @return bool
     */
    public function dispatch(array $options): bool
    {
        $driver = $this->driverFactory->get($options['queue']);
        return $driver->push(
            $options['data'],
            $options['delay'] ?? 0,
        );
    }
}
